package com.zx.disneyholidays.page.coupon.list;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.tencent.mmkv.MMKV;
import com.zx.disneyholidays.MyApplication;
import com.zx.disneyholidays.R;
import com.zx.disneyholidays.bean.CardBean;
import com.zx.disneyholidays.bean.TicketCardBean;
import com.zx.disneyholidays.net.BaseResponse;
import com.zx.disneyholidays.net.HttpUtils;
import com.zx.disneyholidays.net.ResponseCallBack;
import com.zx.disneyholidays.utils.BaseUtil;
import com.zx.disneyholidays.utils.StatusBarUtils;

import java.util.List;

public class CouponListActivity extends AppCompatActivity {

    ListAdapter adapter;
    RecyclerView rvList;

    ImageView ivHeader;

    public static String ticketId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_list);

        StatusBarUtils.setColor(this, Color.WHITE);
        StatusBarUtils.setTextDark(this, true);

        ivHeader = findViewById(R.id.bg_header);

        rvList = findViewById(R.id.rv_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setAutoMeasureEnabled(true);
        rvList.setLayoutManager(layoutManager);
        adapter = new ListAdapter(this);
        rvList.setAdapter(adapter);


        handleEvents();

        requestCouponList();
    }

    private void handleEvents() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CouponListActivity.this.finish();
            }
        });
    }


    private void requestCouponList() {
        HttpUtils.instance.httpGet("api/index/coupon_list", null, new ResponseCallBack() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (data.getCode() != 1) {
                    Toast.makeText(MyApplication.applicationContext, data.getMsg(), Toast.LENGTH_SHORT).show();
                    return;
                }

                try {
                    TicketCardBean cardBean = JSONObject.parseObject(data.getData(), TicketCardBean.class);
                    bindView(cardBean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(int error, String msg) {
                Toast.makeText(MyApplication.applicationContext, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    boolean expend = true;

    @SuppressLint("SetTextI18n")
    private void bindView(TicketCardBean cardBean) {
        if (cardBean == null
                || cardBean.getCoupon() == null
                || cardBean.getCoupon().size() == 0
                || cardBean.getTicket() == null) {
            return;
        }

        adapter.setData(cardBean.getCoupon());
        ticketId = cardBean.getTicket().getId();

        String name = MMKV.defaultMMKV().decodeString("userName");
        if (!TextUtils.isEmpty(name)) {
            String secondName = name.substring(1, name.length());
            ((TextView) findViewById(R.id.tv_card_name)).setText(secondName + " * " + cardBean.getTicket().getName());
        }

        ((TextView) findViewById(R.id.tv_card_number)).setText(cardBean.getTicket().getCard_no());
        ((TextView) findViewById(R.id.tv_coupon_list_name)).setText("年卡权益（" + cardBean.getCoupon().size() + "）");


        findViewById(R.id.view_expend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (expend) {
                    //收起
                    ((ImageView) findViewById(R.id.iv_expend)).setImageResource(R.mipmap.ic_expend_down);
                    rvList.setVisibility(View.GONE);
                    ((ImageView)findViewById(R.id.bg_header)).setImageResource(R.mipmap.bg_coupon_list_header2);
                    expend = false;
                } else {
                    //展开
                    ((ImageView) findViewById(R.id.iv_expend)).setImageResource(R.mipmap.ic_expend);
                    rvList.setVisibility(View.VISIBLE);
                    ((ImageView)findViewById(R.id.bg_header)).setImageResource(R.mipmap.bg_coupon_list_header);
                    expend = true;
                }
            }
        });

    }


    // 字体大小不跟随系统
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(getConfigurationContext(newBase));
    }

    private static Context getConfigurationContext(Context context) {
        Configuration configuration = context.getResources().getConfiguration();
        configuration.fontScale = 1;
        return context.createConfigurationContext(configuration);
    }


    public Bitmap getRoundedCornerBitmap(Context context, int resourceId, int cornerRadius) {
        // 获取原始的位图对象
        Bitmap source = BitmapFactory.decodeResource(context.getResources(), resourceId);

        int width = source.getWidth();
        int height = source.getHeight();
        Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);

        // 创建一个Paint对象，用于绘制圆角
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

        // 创建一个Path对象，定义圆角矩形的形状
        Path path = new Path();
        path.moveTo(0, 0);
        // 左上角圆角
        path.arcTo(new RectF(0, 0, cornerRadius * 2, cornerRadius * 2), 180, 90, true);
        // 右上角圆角
        path.arcTo(new RectF(width - cornerRadius * 2, 0, width, cornerRadius * 2), 270, 90, true);
        // 连接到起始点，形成闭合路径
        path.lineTo(width, 0);
        path.lineTo(width, height);
        path.lineTo(0, height);
        path.close();

        // 使用Path和Paint在Canvas上绘制圆角矩形
        canvas.drawPath(path, paint);

        return result;
    }

 /*   @Override
    public Resources getResources() {
        Resources res=super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics());
        return res;
    }*/
}